<?php

defined('SYSPATH') or die('No direct script access.');

class Ut {

    private static $instances;
    private $data;

    public static function & instances($id = 0) {

        if (!isset(self::$instances[$id])) {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id) {

        $this->data = NULL;
        $this->_load($id);
    }

    public function _load($id) {

        if (!$id)
            return FALSE;

        $result = ORM::factory('type')
                ->where('id_type', '=', $id)
                ->find()
                ->as_array();

        $this->data = $result ? $result : array();
    }

    public function get($key = NULL) {

        if (is_null($key)) {
            return $this->data;
        } else {
            return isset($this->data[$key]) ? $this->data[$key] : NULL;
        }
    }

    public function set($data) {

        $type = ORM::factory('type');
        $type->values($data);
        if ($type->check()) {
            $type->save();
            if ($type->saved()) {
                return true;
            }
        }

        return false;
    }

    public function delete() {
        $type = ORM::factory('user_ut');
        if ($type->loaded()) {
            return $type->delete();
        } else {
            return FALSE;
        }
    }

    public function get_uts() {
        $uts = ORM::factory('user_ut')->find_all()->as_array();
        $admintype = array();
        foreach ($uts as $value) {
            $admintype[] = $value->AUT_ID;
        }
        return $admintype;
    }

}

